<?php

/**
 * Simon's graphs - GBussines.php (class for bussines graphs)
 *
 * Copyright (c) 2008 Jan ŠTAFA aka Simon (http://betaverze.blogspot.com)
 *
 * For more information please see http://code.google.com/
 *
 * @copyright  Copyright (c) 2008 Jan Štafa aka Simon
 * @license    GNU General Public License
 * @link       http://code.google.com/
 * @category   Graphs
 */
	
require_once 'Graph.php';

class GBussines extends Simon_Graph
{
	/**
     * Definition type of graphs
     */
    const COURSE  = 'lc'; 
    
    
    /**
     * Definition currency
     */
    const EUR  = 'EUR'; 

	
	private function _getDataUrl(){
		
		$dataUrl = "http://www.csas.cz/banka/portlets/exchangerates/history.do?csv=1&event=hist";
		
		$dataUrl.= "&cur=".$this->__get("currency");
		
		$dataUrl.= "&day1=".date("j",strtotime($this->__get("dateFrom")));
		$dataUrl.= "&month1=".date("n",strtotime($this->__get("dateFrom")));
		$dataUrl.= "&year1=".date("Y",strtotime($this->__get("dateFrom")));
		
		$dataUrl.= "&day2=".date("j",strtotime($this->__get("dateTo")));
		$dataUrl.= "&month2=".date("n",strtotime($this->__get("dateTo")));
		$dataUrl.= "&year2=".date("Y",strtotime($this->__get("dateTo")));
		
    	return $dataUrl;
		
	}
	
	
	public function getGraphUrl(){
		
		 $data = file_get_contents($this->_getDataUrl());
         $dataArray = explode("\n",$data);

         for ($i=3;$i<sizeof($dataArray);$i++) {
         
         $dataArray[$i] = trim($dataArray[$i],'"');
         $dataArray[$i] = trim($dataArray[$i],',');
          $data = explode('","',$dataArray[$i]);
          if($data[0] =="Platnost od:"){
              
              break;
          }
               $curencyArray[] = $dataArray[$i];
         
         }
         
         $i=0;
         foreach ($curencyArray as $value){
              $value = trim($value,'"');
              $data = explode('","',$value);
              if($data[7]=="-"){
              $curency[$i] = str_replace(array(","),array("."),$data[6]);
              }else{
               $curency[$i] = str_replace(array(","),array("."),$data[7]);   
              }
           $i++;  
         }
         
         $min = floor(min($curency));
         $max = floor(max($curency))+1;
         
         foreach ($curency as $value){
             if($value>$max-1){
                 $gdata[]=50+$value[3]*5+$value[4]/2;
             }else{
                 $gdata[]=0+$value[3]*5+$value[4]/2;; 
             }
         }
         
         $this->setGraphData($gdata);
         
         
         $i=$min;
         while($i<=$max){
              
              $labelY[]=$i;        
             $i=$i+0.1;
         }
          $labelY[]=$max;
         
        
          return  $this->getUrl();
        
	}
   
}
